package org.ourguy.mymoviesandshows;

import org.ourguy.mymoviesandshows.datamodel.ITitle;
import org.ourguy.mymoviesandshows.datamodel.Series;
import org.ourguy.mymoviesandshows.managers.DataAccessManager;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class ShowGeneralInfoActivity extends Activity {

	private ITitle show;
	private DataAccessManager menager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_general_info);
		
		menager = DataAccessManager.getInstance(this);
		show = menager.getTitle(getIntent().getStringExtra("show"));
		menager.addSeasonsToShow(show);
		
		ImageView imageView = (ImageView)findViewById(R.id.showImage);	
		imageView.setImageBitmap(show.getCoverArt().getThumbnail());
		
		TextView showTitle = (TextView)findViewById(R.id.showTitle);
		showTitle.setText(show.getTitle());
		
		TextView showAirTime = (TextView)findViewById(R.id.showAirTime);
		showAirTime.setText(((Series)show).getAirDay() + " " + ((Series)show).getAirTime());
		
		TextView showOverview = (TextView)findViewById(R.id.showOverview);
		showOverview.setText(((Series)show).getPlot());
		
		String str[] = ((Series)show).getGenre();
		if(str != null)
		{
		TextView genre = (TextView)findViewById(R.id.genre);
		String genres = str[0];
		for(int i=1; i< str.length ; i++)
			genres = genres + ", " + str[i];
		genre.setText("Genre: " + genres);
		}
		
		TextView country = (TextView)findViewById(R.id.country);
		country.setText("Country: " + ((Series)show).getCountry());
		
		RatingBar raitung = (RatingBar)findViewById(R.id.ratingBar1);
		raitung.setRating(((Series)show).getRating());
		
		CheckBox isFavourite = (CheckBox)findViewById(R.id.showSelected);
		isFavourite.setChecked(show.isInFavorites());
		isFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
				{menager.addToFavorites(show, ShowGeneralInfoActivity.this);}
				else
					menager.removeFromFavorites(show, ShowGeneralInfoActivity.this);
			}
		});
		
		
		ImageButton imdbButton = (ImageButton)findViewById(R.id.imdbButton);
		OnClickListener listener = new OnClickListener() {
			
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.imdb.com/title/" + show.getImdbId()));
				startActivity(browserIntent);
			}
		};
		imdbButton.setOnClickListener(listener);
	}
}
